﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyTime.Core;

namespace MyTime.CsvProvider.Entities
{
    class CsvTimeItem : MyTime.ProviderFactory.Entities.ITimeItem
    {

        public CsvTimeItem(string task, DateTime startTime, DateTime endTime, int id)
        {
            // TODO: Complete member initialization
            this.Id = id;
            this.Task = task;
            this.StartTime = startTime;
            this.EndTime = endTime;
        }
        public CsvTimeItem(string task, DateTime startTime, int id,bool isCurrent)
        {
            // TODO: Complete member initialization
            this.Id = id;
            this.Task = task;
            this.StartTime = startTime;
            this.Current = isCurrent;
        }
        public int Id { get; set; }

        public string Task { get; set; }

        public DateTime StartTime { get; set; }

        public DateTime? EndTime { get; set; }

        public TimeSpan TotalTime
        {
            get
            {
                if (EndTime == null || EndTime == DateTime.MinValue)
                    return DateTime.Now.Subtract(StartTime);
                return EndTime.Value.Subtract(StartTime);
            }
            set { throw new NotImplementedException(); }
        }

        public bool Current { get; set; }
        public int ProjectId { get; set; }
        public string ProjectName { get; set; }

        public int WeekNumber
        {
            get { return TimeHelper.GetWeekNumber(StartTime); }
            set { throw new NotImplementedException(); }
        }

        public static MyTime.ProviderFactory.Entities.ITimeItem Parse(string[] row, int rowNr)
        {
            if (row.Length != 3)
                throw new ArgumentOutOfRangeException("cols", row, "columns should be task, startTime, endTime");
            var task = row[0];
            var startTime = DateTime.Parse(row[1]);
            var endTime = DateTime.Now;
            if (DateTime.TryParse(row[2], out endTime))
                return new CsvTimeItem(task, startTime, endTime, rowNr);

            //no endtime it must be current time item
            return new CsvTimeItem(task, startTime, rowNr, true);
        }
    }
}
